package com.example.videopublish.activity

import android.accessibilityservice.AccessibilityServiceInfo
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.provider.Settings
import android.view.accessibility.AccessibilityManager
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.videopublish.R
import com.example.videopublish.service.DouyinRpaService

/**
 * 应用程序主活动
 * 提供视频选择入口和无障碍服务状态检查
 */
class MainActivity : AppCompatActivity() {

    private lateinit var selectVideoButton: Button
    private lateinit var serviceStatusTextView: TextView
    private lateinit var accessibilitySettingsButton: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // 初始化视图
        initViews()
        
        // 检查无障碍服务状态
        updateAccessibilityServiceStatus()
    }

    override fun onResume() {
        super.onResume()
        // 每次恢复活动时检查服务状态
        updateAccessibilityServiceStatus()
    }

    /**
     * 初始化视图并设置点击监听器
     */
    private fun initViews() {
        // 获取视图引用
        selectVideoButton = findViewById(R.id.selectVideoButton)
        serviceStatusTextView = findViewById(R.id.serviceStatusTextView)
        accessibilitySettingsButton = findViewById(R.id.accessibilitySettingsButton)

        // 设置视频选择按钮点击监听
        selectVideoButton.setOnClickListener {
            // 启动视频选择活动
            startActivity(Intent(this, VideoSelectionActivity::class.java))
        }

        // 设置无障碍服务设置按钮点击监听
        accessibilitySettingsButton.setOnClickListener {
            // 打开无障碍服务设置界面
            startActivity(Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS))
        }
    }

    /**
     * 检查无障碍服务状态并更新UI
     */
    private fun updateAccessibilityServiceStatus() {
        if (isAccessibilityServiceEnabled()) {
            // 服务已启用
            serviceStatusTextView.text = "无障碍服务已启用"
            serviceStatusTextView.setTextColor(getColor(android.R.color.holo_green_dark))
            accessibilitySettingsButton.text = "无障碍服务已启用"
            accessibilitySettingsButton.isEnabled = false
        } else {
            // 服务未启用
            serviceStatusTextView.text = getString(R.string.enable_accessibility_service)
            serviceStatusTextView.setTextColor(getColor(android.R.color.holo_red_dark))
            accessibilitySettingsButton.text = getString(R.string.enable_accessibility_service)
            accessibilitySettingsButton.isEnabled = true
        }
    }

    /**
     * 检查我们的无障碍服务是否已启用
     */
    private fun isAccessibilityServiceEnabled(): Boolean {
        val accessibilityManager = getSystemService(Context.ACCESSIBILITY_SERVICE) as AccessibilityManager
        val enabledServices = accessibilityManager.getEnabledAccessibilityServiceList(
            AccessibilityServiceInfo.FEEDBACK_ALL_MASK
        )
        
        // 检查我们的服务是否在已启用的服务列表中
        for (service in enabledServices) {
            if (service.id.contains(packageName) && service.id.contains(DouyinRpaService::class.java.simpleName)) {
                return true
            }
        }
        return false
    }
} 